package net.huashitong.gradePoint.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.gradePoint.databean.DepartRank;
import net.huashitong.gradePoint.entity.ScoreSummary;
import net.huashitong.gradePoint.filter.ScoreSummaryFilter;
import net.huashitong.gradePoint.service.ScoreSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/scoresummary")
public class ScoreSummaryCtrl extends JsonBaseController {

    @Override
    protected String getView(String s) {
        return "/modules/sys/scoresummary/" + s;
    }

    @Autowired
    private ScoreSummaryService service;

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        return getView("query");
    }


    //初始化数据
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute ScoreSummaryFilter filter) {

        if (filter.isDepartRank()) {
            if(StringUtils.isNotBlank(filter.getPaging().getSortVal())){
                filter.getPaging().setSortVal("");
            }
            List<DepartRank> list = service.queryDepartRank(filter);
            int num = (filter.getPaging().getCurrentPage() - 1) * 10 + 1;
            for (DepartRank d : list) {
                double x = (double) Math.round(d.getScore() * 100) / 100;
                d.setScore(x);
                d.setRank(num+"");
                d.setCheckDept("——");
                num++;
            }
            return coverJqGrid(filter, list);
        }
        List<ScoreSummary> list = service.queryByFilter(filter);
        if (StringUtils.isBlank(filter.getBranchType())) {
            return coverJqGrid(filter, list);
        } else {
            for (ScoreSummary summary : list) {
                summary.setRank(summary.getTypeRank());
            }
            return coverJqGrid(filter, list);
        }
    }

    @RequestMapping("/filter")
    public String filter() {
        //所有部门
        return getView("filter");
    }

//    @RequestMapping("/formHead")
//    public String showHead(@ModelAttribute NodeNewschedulesFilter filter, Model model) {
//        return getView("formHead");
//    }

}